import { Role } from "../domain/models/role";
import { BaseService, QueryParamsBase } from "./baseService";
import { ApiDataContext } from "../domain/apiDataContext";
import { Transaction } from "tiny-entity2";
import { IQueryObject } from "tiny-entity2/queryObject";
import { OrganizationService } from './organizationService';
import { ServiceManager } from "../common";
import { OrganizationTree } from "../domain/models/organizationTree";
import { Organization } from "../domain/models/organization";
const orgSvr = ServiceManager.Current.GetService<OrganizationService>('OrganizationService');

export class RoleService extends BaseService {
    async CreateRole(entity: Role, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Role();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    async UpdateRole(entity: Role, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Role();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    async DeleteRole(entity: Role, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Role();
            d.ConverToEntity(entity);
            return ctx.Delete(d);
        });
    }
    async GetRoleById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.Role.First(x => x.id == $args1, { $args1: id });
    }
    async QueryRole(params) {
        let q =await this.GetQueryRoleCtx(params);
        let total = await q.Count();

        let q2 =await this.GetQueryRoleCtx(params);
        if (params.countOfPage && params.pageIndex) {
            q2 = q2.Take(params.countOfPage).Skip(params.pageIndex - 1) as any;
        }
        let list = await q2.ToList();

        return {
            total,
            list
        };
    }

    private async GetQueryRoleCtx(params: RoleQueryParams) {
        let ctx = new ApiDataContext();
        let q = ctx.Role as IQueryObject<Role>;
        q = q.Where(x => x.isDelete == $args1, { $args1: false});
        if (params) {
            if (params.name) {
                q = q.Where(x => x.name.indexOf($args1), { $args1: params.name });
            }
            if (params.organizationId) {
                q = q.Where(x => x.organizationId.indexOf($args1), { $args1: params.organizationId });
            }
            if (params.organizationName) {
              let orglist:Organization[]=(await orgSvr.QueryOrganization({name:params.organizationName})).list;
              q = q.Join(ctx.Organization).On((m,f)=>m.organizationId==f.id);
            }
            if (params.isGlobal) {
                q = q.Where(x => x.isGlobal==$args1, { $args1: params.isGlobal });
            }
        }
        return q;
    }
}

export interface RoleQueryParams extends QueryParamsBase {
    name?: string;
    organizationId?: string;
    organizationName?: string;
    isGlobal?: boolean;
}